//
//  ViewController.m
//  UI01_homework 第三题
//
//  Created by dllo on 16/6/4.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addView];
}


-(void)addView {
    CGFloat weidth = self.view.frame.size.width / 5;
    CGFloat height = self.view.frame.size.height / 8;
    for (int i = 1; i <= 8; i++) {
        for (int j = 1; j <= 5; j++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(weidth * (j - 1), height * (i - 1), weidth, height);
            button.backgroundColor = [self colorOfRandom];
            
          
            [self.view addSubview:button];
            [button addTarget:self action:@selector(colorOfRandom:) forControlEvents:UIControlEventTouchUpInside];
            [button release];
        }
    }
}

-(UIColor *)colorOfRandom
{
    int R = arc4random() % 256;
    int G = arc4random() % 256;
    int B = arc4random() % 256;
    return [UIColor colorWithRed:R/255.0  green:G/255.0  blue:B/255.0  alpha:1];
}


-(void)colorOfRandom:(UIButton *)button
{
    NSArray *array = button.superview.subviews;
    NSEnumerator *enrmerator = [array objectEnumerator];
    UIButton *object = [UIButton buttonWithType:UIButtonTypeCustom];
    
    while (object = [enrmerator nextObject]) {
        object.backgroundColor = [self colorOfRandom];
        
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
